# Este script cria um arquivo tar, empacotando os backups
# indexados no array.
#
#tar tvzf /var/backups/omicron2/pg_omicron2_backup.tar.gz 
#-rw-r--r-- root/root 327920009 2010-03-23 12:10 pg_agcrises_omicron2_20100323-1210.backup
#-rw-r--r-- root/root   3170060 2010-03-23 12:10 pg_asprev_omicron2_20100323-1210.backup
#-rw-r--r-- root/root 4570767085 2010-03-23 12:17 pg_igeopr_omicron2_20100323-1210.backup
#
#
#
#


HOST=`uname -n`;
USER=postgres
BKPDIR=/var/backups/omicron2
BKPFILE=pg_${HOST}_backup.tar.gz

#DATABASE=( "agcrises" "asprev" "igeopr" );

DATABASE[0]="agcrises"
DATABASE[1]="asprev"
DATABASE[2]="igeopr"

#echo ${DATABASE[0]}
#echo ${DATABASE[2]} 
#echo ${#DATABASE[@]}

export DATE HOST USER BKPDIR FILE DATABASE

#echo $HOST

#echo cd $BKPDIR/
cd $BKPDIR/;

#echo ------------------------ check pwd `pwd` ----------------------------------------

#echo "rm [ -e ${BKPFILE} ] && rm ${BKPFILE}"

[ -e ${BKPFILE} ] && rm ${BKPFILE};

for ((i=0; i<${#DATABASE[@]}; i++)); 
  do 
	#echo ${DATABASE[$i]}
	#teste criação pg_dump
	#echo pg_${DATABASE[$i]}_${HOST}_${DATE}.backup > pg_${DATABASE[$i]}_${HOST}_${DATE}.backup
	
	#gera pg_dump dos database
	#echo "pg_dump -vh ${HOST} -U ${USER} ${DATABASE[$i]} >> pg_${DATABASE[$i]}_${HOST}_${DATE}.backup"
	pg_dump -h ${HOST} -U ${USER} ${DATABASE[$i]} >> pg_${DATABASE[$i]}_${HOST}_${DATE}.backup
  done;
#Cria tar.bz com arquivos.backup
tar cvzf ${BKPFILE} pg_*_${HOST}_${DATE}.backup

#remove arquivos.backup
#echo rm -f *.backup
rm -f *.backup

cd -;
unset DATE HOST USER BKPDIR FILE DATABASE;


####### envia para maquina remota
scp /var/backups/omicron2/pg_omicron2_backup.tar.gz usbackup@10.100.0.30:/var/backups/omicron2/$(date +%Y%m%d)
